package com.chenfan.process.enums;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import lombok.Getter;

/**
 * @author K
 * @date 2021/3/9 执行的结果类型
 */
@Getter
public enum ProcessResultTypeEnums {
    /**
     * 已提交
     */
    PARENT(0, "已提交"),
    /**
     * 审批中
     */
    CREATED(1, "审批中"),
    /**
     * 审批通过
     */
    PASS(2, "审批通过"),
    /**
     * 审批拒绝
     */
    REFUSED(3, "审批拒绝"),
    /**
     * 审批结束
     */
    END(4, "审批结束"),
    /**
     * 已撤回
     */
    REVOKE(5, "已撤回"),
    /**
     * 已驳回
     */
    REJECTED(6, "已驳回"),;

    private static final Map<Integer, ProcessResultTypeEnums> MAPPINGS;

    static {
        Map<Integer, ProcessResultTypeEnums> temp = new HashMap<>();
        for (ProcessResultTypeEnums mode : values()) {
            temp.put(mode.index, mode);
        }
        MAPPINGS = Collections.unmodifiableMap(temp);
    }

    private final Integer index;
    private final String name;

    ProcessResultTypeEnums(int index, String name) {
        this.index = index;
        this.name = name;
    }

    public static ProcessResultTypeEnums resolve(int index) {
        return MAPPINGS.get(index);
    }

    public static String getMessage(Integer resultTypeValue) {
        for (ProcessResultTypeEnums processResultTypeEnums : values()) {
            if (processResultTypeEnums.getIndex().equals(resultTypeValue)) {
                return processResultTypeEnums.getName();
            }
        }
        return "";
    }

    public static boolean processing(Integer process) {
        if (PARENT.getIndex().equals(process) || CREATED.getIndex().equals(process) || END.getIndex().equals(process)) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    public static Integer getApprovalResult(Boolean approvalFlag) {
        return Boolean.TRUE.equals(approvalFlag) ? ProcessResultTypeEnums.PASS.getIndex()
            : ProcessResultTypeEnums.REFUSED.getIndex();
    }
}
